Home:ALL Converter>Run powershell module from python in ubuntu

Run powershell module from python in ubuntu

Ask Time:2020-09-09T22:51:45         Author:Maria

Json Formatter

I have PowerShell installed on ubuntu 20.04 and I want to do the following steps from the python script:

  • Invoke it
  • Import PowerShell module
  • Run Powershell module several times

From the terminal, I would do it as:

  • pwsh
  • Import-Module ./MyModule.psd1
  • MyModule -param 'param_val' -Quiet > filename several times

I was trying to use

p = subprocess.Popen("pwsh", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)
time.sleep(2)
p.stdin.write("Import-Module ./MyModule.psd1")
#in loop
p.stdin.write("MyModule -param 'param_val' -Quiet > filename")

Which is obviously not working. I tried to use Thread and Queue but I am afraid that the problem is not in it. I would be extremely grateful for any help and suggestions.

Author:Maria,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/63814224/run-powershell-module-from-python-in-ubuntu
yy